﻿//===================================================================
//
// Description: Sparqube Picture Column Lite for SharePoint 2010
// Web: http://www.sparqube.com/
//
//-------------------------------------------------------------------
//
// Copyright (C) 2012 ArtfulBits.CZ (development[at]artfulbits.cz)
// Web: http://www.artfulbits.cz/
//
//-------------------------------------------------------------------
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A 
// PARTICULAR PURPOSE.
//===================================================================

using System;
using System.Web.UI;
using System.Web.UI.WebControls;

using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

namespace Sparqube.SharePoint.PictureColumnLite
{
	public partial class PictureLiteFieldEditor :
		UserControl,
		IFieldEditor
	{
		#region IFieldEditor Implementation

		public bool DisplayAsNewSection
		{
			get { return true; }
		}

		public void InitializeWithField(SPField field)
		{
			if (!this.Page.IsPostBack)
			{
				PictureLiteField pictureField = field as PictureLiteField;
				TxtStorageListUrl.Text = (null != pictureField) ? pictureField.StorageFolderUrl : "";
				int imgWidth = (null != pictureField) ? pictureField.ThumbnailWidth : PictureLiteField.Default_ThumbnailWidth;
				int imgHeight = (null != pictureField) ? pictureField.ThumbnailHeight : PictureLiteField.Default_ThumbnailHeight;
				TextImageWidth.Text = (0 < imgWidth) ? imgWidth.ToString() : "";
				TextImageHeight.Text = (0 < imgHeight) ? imgHeight.ToString() : "";
			}
		}

		public void OnSaveChange(SPField field, bool isNewField)
		{
			PictureLiteField pictureField = (PictureLiteField)field;

			string storagePath = TxtStorageListUrl.Text;
			SPFolder folder = PictureUploader.GetPictureStorageFolder(TxtStorageListUrl.Text, SPContext.Current.Web);
			if (null != folder)
			{
				storagePath = folder.Url;
			}
			pictureField.Adapter.SetProperty(PictureLiteField.Property_StorageFolderUrl, storagePath, isNewField);

			int imgWidth = GetImageSizeValue(TextImageWidth.Text);
			pictureField.Adapter.SetProperty(PictureLiteField.Property_ThumbnailWidth, imgWidth, isNewField);
			int imgHeight = GetImageSizeValue(TextImageHeight.Text);
			pictureField.Adapter.SetProperty(PictureLiteField.Property_ThumbnailHeight, imgHeight, isNewField);
		}

		#endregion //IFieldEditor Implementation

		#region Event Handlers

		protected void ValidateImageSize(object source, ServerValidateEventArgs args)
		{
			if (source == null)
			{
				throw new ArgumentNullException("source");
			}
			if (args == null)
			{
				throw new ArgumentNullException("args");
			}

			InputFormCustomValidator validator = (InputFormCustomValidator)source;
			bool isValid = ValidateTextBoxPositiveInteger(TextImageWidth, validator) &&
				ValidateTextBoxPositiveInteger(TextImageHeight, validator);
			args.IsValid = isValid;
		}

		protected void ValidatorStorageList_ServerValidate(object source, ServerValidateEventArgs args)
		{
			InputFormCustomValidator validator = (InputFormCustomValidator)source;
			if (string.IsNullOrEmpty(TxtStorageListUrl.Text))
			{
				validator.ErrorMessage = ResourceManager.GetResource(EResource.Error_PictureStorageShouldBeSpecified);
				args.IsValid = false;
			}
			else if (!string.IsNullOrEmpty(TxtStorageListUrl.Text))
			{
				SPFolder folder = PictureUploader.GetPictureStorageFolder(TxtStorageListUrl.Text, SPContext.Current.Web);
				if (null == folder)
				{
					validator.ErrorMessage = ResourceManager.GetResource(EResource.Error_PictureStoragePathIsNotValid, TxtStorageListUrl.Text);
					args.IsValid = false;
				}
			}
		}

		#endregion //Event Handlers

		#region Implementation

		private bool ValidateTextBoxPositiveInteger(TextBox textBox, InputFormCustomValidator validator)
		{
			bool result = true;
			string text = textBox.Text;
			if (!string.IsNullOrEmpty(text))
			{
				int val = 0;
				if (!int.TryParse(text, out val) || (val <= 0))
				{
					validator.ErrorMessage = ResourceManager.GetResource(EResource.Error_ValueShouldBeEmptyOrPositiveInteger);
					result = false;
					textBox.Focus();
				}
			}
			return result;
		}

		private int GetImageSizeValue(string text)
		{
			int result = 0;
			int val;
			if (int.TryParse(text, out val) && (0 < val))
			{
				result = val;
			}
			return result;
		}

		#endregion //Implementation

	}// PictureLiteFieldEditor
}
